<?php
// +----------------------------------------------------------------------
// | Azthink通用开发平台 [ TOMORROW IS ANOTHER DAY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 http://www.azthink.top All rights reserved.
// +----------------------------------------------------------------------
// | Author 李忠<azthink@qq.com>
// +----------------------------------------------------------------------
namespace app\common\controller;

use app\common\model\File as FileModel;
use think\Controller;
use think\Exception;

/**
 * 文件控制器
 *
 * @author 李忠<azthink@qq.com>
 * @createAt 2018-09-12 16:08:42
 *
 */
class File extends Controller {

    public function upload() {

        if ($this->request->isPost()) {

            $file = request()->file('file');
            try {
                $data = FileModel::upload($file, config('attachment_upload'));
                return json(array('result' => 'ok', 'id' => $data['id']));
            } catch (Exception $e) {
                return json(array('result' => 'failed', 'message' => $e->getMessage()));
            }

        } else {

            return $this->fetch('demo');
        }
    }

    public function download($id) {
        $file = FileModel::get($id);
        //dump($file['savepath'].$file['savename']);
        return download($file['savepath'] . $file['savename'], $file['name']);
    }
}